<template>
    <div class="weui-media-box__hd">
        <img width="150px" class="weui-media-box__thumb"
             :src="src">
    </div>
</template>

<script>
    import uuidv1 from 'uuid/v1'
    //import {downFile} from "../api/manage";

    export default {
        data() {
            return {
                ws: null,

                src: "/qrcode/wxredirect?uuid="+ uuidv1(),
                auth: false,

            };
        },
        mounted() {
            this.connect();


        },
        methods: {
            connect() {
                console.log(this.src)
                let connectionTime =0;
                const clientID =this.src.substring(this.src.indexOf("=")+1);

                console.log(clientID)
                if (clientID != null) {
                    if ('WebSocket' in window) {

                        this.ws = new WebSocket("ws://"+window.location.host+"/socketServer/" + clientID);
                        connectionTime=Date.now();

                    } else {
                        alert("该浏览器不支持websocket");
                    }
                    this.ws.onmessage = function (evt) {
                        const userRelateAccount = JSON.parse(evt.data)
                        if (userRelateAccount === null) {
                            console.log(evt.data)
                        } else {
                           const login_url = userRelateAccount.loginUrl;
                            const target_url = userRelateAccount.targetUrl;
                            const user = userRelateAccount.appAccount;
                            const psd = userRelateAccount.appPassword;
                            func1(user,psd,login_url,target_url)
                        }
                    };

                    this.ws.onclose = function (evt) {
                        console.log(evt);
                        const time =  Date.now();

                        console.log('连接时间：',time -connectionTime)
                        alert("二维码失效，请刷新页面" )

                    };

                    this.ws.onopen = function (evt) {
                        console.log(evt)
                    };

                } else {
                    alert("clientID异常");
                }
               let func2 = function func3(user,psd,login_url,target_url) {
                    this.commLogin2(user,psd,login_url,target_url)

                }
                let func1 = func2.bind(this)
            },
            commLogin2(username, password, login_url, urlTarget) {
                const data = {"fr_username": username, "fr_password": password};
                this.$http.jsonp(login_url, {
                    params: data,
                    jsonp: 'callback'//jsonp默认是callback,百度缩写成了cb，所以需要指定下                     }
                }).then(res => {
                    console.log("res:",res.data)
                    if (res.data.status=="success") {
                        window.location = urlTarget;//登录成功，直接跳转到平台系统页面
                    } else {
                        alert("用户名密码错误！！！");
                    }
                }).catch(err=>{
                    console.log("err",err)
                   // window.location = urlTarget;
                });
                /*downFile(login_url, data).then(res => {
                    if (res.indexOf('success')) {
                        console.log("success"); //登录成功
                        window.location = urlTarget;//登录成功，直接跳转到平台系统页面
                    } else {
                        alert("用户名密码错误！！！");           //登录失败（用户名或密码错误）
                    }
                }).catch(err => {
                    console.log(err);
                })*/
            },

        },
        watch: {

        }
    }

</script>